package com.jz.netthings.bean.system;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * @author yyy
 * @ClassName JyUser
 * @Description 用户
 * @date 2018/7/26 16:16
 */
@TableName(value = "jy_user")
public class JyUser implements UserDetails {

    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
    private String gender;
    private String idNo;
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date birthday;
    private String phone;
    private String telephone;
    private String address;
    private String wxNo;
    private String qqNo;
    private String photo;
    private String username;
    private String password;
    private Integer deptId;
    private Integer postId;
    private String remark;
    private boolean enabled;
    private Date createTime;
    private Integer unitId;//人员所属单位（经销商、生产厂商）的ID，如果是生产厂商，则 unitId = 0
    @TableField(exist = false)
    private List<JyRole> roles;
    /**
     *  所有能访问的资源，包括：菜单和其他操作的URL
     */
    @TableField(exist = false)
//    @JsonIgnore
    private Map<Integer, JyMenu> menus;

    /**
     * 所有能访问的菜单，只是界面上能看到的菜单
     * 所有菜单都在这里，当前用户有权限看到的菜单，JyMenu 中的 show 字段 为true，没权限访问的菜单，show字段就为false
     * srcType = 1 的所有菜单
     */
    @TableField(exist = false)
    private List<JyMenu> allMenus;
    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (JyRole role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getRole_code()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    /**
     * 账户是否没有过期
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账号是否没有锁定
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否没有过期
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是不是可用
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return this.enabled;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getWxNo() {
        return wxNo;
    }

    public void setWxNo(String wxNo) {
        this.wxNo = wxNo;
    }

    public String getQqNo() {
        return qqNo;
    }

    public void setQqNo(String qqNo) {
        this.qqNo = qqNo;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public List<JyRole> getRoles() {
        return roles;
    }

    public void setRoles(List<JyRole> roles) {
        this.roles = roles;
    }

    public Map<Integer, JyMenu> getMenus() {
        return menus;
    }

    public void setMenus(Map<Integer, JyMenu> menus) {
        this.menus = menus;
    }

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public Integer getPostId() {
        return postId;
    }

    public void setPostId(Integer postId) {
        this.postId = postId;
    }

    public List<JyMenu> getAllMenus() {
        return allMenus;
    }

    public void setAllMenus(List<JyMenu> allMenus) {
        this.allMenus = allMenus;
    }

    public Integer getUnitId() {
        return unitId;
    }

    public void setUnitId(Integer unitId) {
        this.unitId = unitId;
    }

    @Override
    public String toString() {
        return "JyUser{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", idNo='" + idNo + '\'' +
                ", birthday=" + birthday +
                ", phone='" + phone + '\'' +
                ", telephone='" + telephone + '\'' +
                ", address='" + address + '\'' +
                ", wxNo='" + wxNo + '\'' +
                ", qqNo='" + qqNo + '\'' +
                ", photo='" + photo + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", deptId=" + deptId +
                ", postId=" + postId +
                ", remark='" + remark + '\'' +
                ", enabled=" + enabled +
                ", createTime=" + createTime +
                ", unitId=" + unitId +
                ", roles=" + roles +
                ", menus=" + menus +
                ", allMenus=" + allMenus +
                '}';
    }
}
